﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Staticage.Util {
    public static class IOHelper {

        /// <summary>
        /// Copies one stream into another. 
        /// (Don't forget to call <see cref="Stream.Flush"/> on the destination stream!)
        /// </summary>
        /// <remarks>
        /// Does not close the input stream!
        /// </remarks>
        public static void CopyStream(Stream source, Stream destination) {
            int bufferSize = 2048;
            byte[] buffer = new byte[bufferSize];

            int bytesRead = 0;
            while ((bytesRead = source.Read(buffer, 0, bufferSize)) > 0) {
                destination.Write(buffer, 0, bytesRead);
            }
        }

        /// <summary>
        /// Reads a stream into a byte array. 
        /// </summary>
        /// <remarks>
        /// Does not close the input stream!
        /// </remarks>
        public static byte[] ToByteArray(Stream source) {
            MemoryStream stm = new MemoryStream();
            CopyStream(source, stm);
            stm.Close();
            return stm.ToArray();
        }
    }
}
